#include "vm.h"

#define VM_BANK_COUNT	(2)
#define VM_BANK_SIZE	(1<<16)

u8 vm_bank;

u8 vm_memory[VM_BANK_COUNT][VM_BANK_SIZE];

void vm_setBank(u8 bank)
{
	vm_bank = bank;
}

u8 vm_getBank()
{
	return vm_bank;
}

void vm_write(VMPtr ptr, void* buf, u16 n)
{
	for (u16 i=0;i<n;i++)
	{
		vm_memory[vm_bank][ptr+i] = ((u8*)buf)[i];
	}
}

void vm_writeU8(VMPtr ptr, u8 v)
{
	vm_write(ptr, &v, sizeof(v));
}

void vm_writeS8(VMPtr ptr, s8 v)
{
	vm_write(ptr, &v, sizeof(v));
}

void vm_writeU16(VMPtr ptr, u16 v)
{
	vm_write(ptr, &v, sizeof(v));
}

void vm_writeS16(VMPtr ptr, s16 v)
{
	vm_write(ptr, &v, sizeof(v));
}

void vm_writeU32(VMPtr ptr, u32 v)
{
	vm_write(ptr, &v, sizeof(v));
}

void vm_writeS32(VMPtr ptr, s32 v)
{
	vm_write(ptr, &v, sizeof(v));
}


void vm_read(VMPtr ptr, void* buf, u16 n)
{
	for (u16 i=0;i<n;i++)
	{
		((u8*)buf)[i] = vm_memory[vm_bank][ptr+i];
	}
}

s8 vm_readS8(VMPtr ptr)
{
	s8 v;
	vm_read(ptr, &v, sizeof(v));
	return v;
}

u8 vm_readU8(VMPtr ptr)
{
	u8 v;
	vm_read(ptr, &v, sizeof(v));
	return v;
}

s16 vm_readS16(VMPtr ptr)
{
	s16 v;
	vm_read(ptr, &v, sizeof(v));
	return v;
}

u16 vm_readU16(VMPtr ptr)
{
	u16 v;
	vm_read(ptr, &v, sizeof(v));
	return v;
}

s32 vm_readS32(VMPtr ptr)
{
	s32 v;
	vm_read(ptr, &v, sizeof(v));
	return v;
}

u32 vm_readU32(VMPtr ptr)
{
	u32 v;
	vm_read(ptr, &v, sizeof(v));
	return v;
}